package com.manbearpig.todue;

import java.util.Date;
import java.text.SimpleDateFormat;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.RadioButton;
import android.widget.Spinner;

/**
 * This class enables filter settings
 * @author Brandon Devereaux
 * @version 1.0
 */
public class FilterActivity extends Activity
{
	public String username;
	/**
	 * Method called when the activity is created
	 */
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.filterlayout);
	    
	    Bundle bundle = this.getIntent().getExtras();
        this.username = (String)bundle.get("Username");
	    
	    Spinner spinner = (Spinner)findViewById(R.id.category_filter_spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.categories, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
	}
	
	/**
	 * Cancels application of a filter
	 * @param view the requested view
	 */
	public void cancelFilter(View view)
	{
		Intent myIntent = new Intent(this, ToDoListActivity.class);
	    myIntent.putExtra("Username", this.username);
		this.finish();
	    startActivity(myIntent);	
	}
	
	/**
	 * This method passes the filter settings to the list view
	 * @param view the requested view
	 */
	public void applyFilter(View view)
	{
		Intent myIntent = new Intent(this, ToDoListActivity.class);
	    myIntent.putExtra("Username", this.username);
	    //fill in other filter parameters as extras
	    
	    boolean filterByDate = ((CheckBox)findViewById(R.id.filterDate)).isChecked();
	    boolean filterByType = ((CheckBox)findViewById(R.id.filterType)).isChecked();
	    boolean filterByComplete = ((CheckBox)findViewById(R.id.filterCompletion)).isChecked();
	    boolean filterBefore = ((RadioButton)findViewById(R.id.beforeOption)).isChecked();
	    boolean filterIncomplete = ((RadioButton)findViewById(R.id.uncompletedOption)).isChecked();
	    String filterCategory = (String)(((Spinner)findViewById(R.id.category_filter_spinner)).getSelectedItem());
		DatePicker dp = ((DatePicker)findViewById(R.id.datepicker_filter));
	    Date tempFilterDate = new Date(dp.getYear() - 1900, dp.getMonth(), dp.getDayOfMonth());
	    SimpleDateFormat formatter = new SimpleDateFormat();
	    formatter.applyPattern("yyyy-MM-dd");
	    String filterDate = formatter.format(tempFilterDate);
	    
	    myIntent.putExtra("filterByDate", filterByDate);
	    myIntent.putExtra("filterByType", filterByType);
	    myIntent.putExtra("filterByComplete", filterByComplete);
	    myIntent.putExtra("filterBefore", filterBefore);
	    myIntent.putExtra("filterIncomplete", filterIncomplete);
	    myIntent.putExtra("filterCategory", filterCategory);
	    myIntent.putExtra("filterDate", filterDate);
	    
	    this.finish();
	    startActivity(myIntent);
	}
}
